package com.pos.app;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

import com.pos.app.activity.base.AppManager;
import com.pos.app.activity.login.interactor.LoginInteractor;
import com.pos.app.activity.login.interactor.LoginInteractorImpl;
import com.pos.app.exception.AppException;
import com.pos.app.util.MyLogger;
import com.pos.app.util.cache.MyFileCache;
import com.pos.app.util.cache.MyFileCache.MyCacheParams;
import com.pos.app.util.ioc.Instance.InstanceScope;
import com.pos.app.util.ioc.IocContainer;
import com.pos.app.util.log.PrintToFileLogger;

/**
 * 全局对象
 * @author Administrator
 *
 */
public class POSApplication extends Application
{
	public static final int NETTYPE_WIFI = 0x01;
	public static final int NETTYPE_CMWAP = 0x02;
	public static final int NETTYPE_CMNET = 0x03;

	private static POSApplication application = null;
	private Context context;
	
	/** 文件缓存 */
	private MyFileCache mFileCache;
	private static final String SYSTEMCACHE = "pos";
	
	/**
	 * 获取应用程序全局对象
	 * @return
	 */
	public static POSApplication getInstance()
	{
		return application;
	}
	
	@Override
	public void onCreate()
	{
		super.onCreate();
		context = this;
		application = this;
		
		// 全局异常
		Thread.setDefaultUncaughtExceptionHandler(AppException.getAppExceptionHandler());
		
		//日志配置
		MyLogger.addLogger(new PrintToFileLogger());
		MyLogger.i(POSApplication.this, "日志初始化！");
		// IOC的初始化
		IocContainer.getShare().initApplication(this);

		// IocContainer.getShare().bind(UserModel.class).to(IUserModel.class);
		IocContainer.getShare().bind(LoginInteractorImpl.class).to(LoginInteractor.class).scope(InstanceScope.SCOPE_SINGLETON);
	}
	
	/**
	 * 退出程序
	 */
	public void exit() {
		AppManager.getInstance().AppExit(context);
	}
	
	/**
	 * 获取App安装包信息
	 * 
	 * @return
	 */
	public PackageInfo getPackageInfo()
	{
		PackageInfo info = null;
		try
		{
			info = getPackageManager().getPackageInfo(getPackageName(), 0);
		}
		catch (NameNotFoundException e)
		{
			e.printStackTrace(System.err);
		}
		if (info == null)
			info = new PackageInfo();
		return info;
	}
	
	public MyFileCache getFileCache()
	{
		if (mFileCache == null)
		{
			MyCacheParams cacheParams = new MyCacheParams(this, SYSTEMCACHE);
			MyFileCache fileCache = new MyFileCache(cacheParams);
			application.setFileCache(fileCache);

		}
		return mFileCache;
	}

	public void setFileCache(MyFileCache fileCache)
	{
		this.mFileCache = fileCache;
	}
}